<?php
$cs = Yii::app()->getClientScript();
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_find_1.css');
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_list.css');
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$wxControl = $screen->getChildControl('BUIWXControl_1');
$venderList = Blyn::app()->getActiveApp()->findVenderListByEventType();
$courtTypeList = Blyn::app()->getActiveApp()->getCourtTypeList();

//if($this->getContextVariable('nowCity')) {
//    $city = $this->getContextVariable('nowCity');
//}else {
    if(isset($nowCity)) {
        $city = $nowCity;//通常是省市拼音，如：china.chongqing
    }else {
        $attribute = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId());
        $city = $attribute->getValue();
    }
//    $this->addContextVariable('nowCity', $city);
//}

$app = Blyn::app()->getActiveApp();
$venderCourtTypeBookableList = array();
?>

<meta name="viewport" contdent="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1" media="(device-height: 568px)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="full-screen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<script>
$("document").ready(function() {
    $(".booking_list > dd").click(function() {
        $(this).css("backgroundColor","rgb(243, 243, 246)");
        $(this).siblings().css("backgroundColor","white");
    });
}); 
</script>
<body id="index">

    <header class="navbar">
        <div class="nav-wrap-left">
            <?php
            $bookingFindUrl = $wxControl->getLinkUrl(array(
                'name' => 'booking_find_1',
                'id' => 'booking_find_1'),array(
                'nowCity' => $city,   //$city=重庆
                'findType' => 'full' 
            ));
            ?>
            <a class="react brand" href="<?php echo $bookingFindUrl;?>">
                <span class="logo">
                    <font style="font-size:20px; font-family: Microsoft YaHei;">场地预订</font>
                </span>
            </a>
            <?php 
            $changeCityUrl = $wxControl->getLinkUrl(array(
                            'name' => '修改城市',
                            'id' => 'booking_change_city')
                    );
            ?>
            <span class="nav-city"><a gaevent="imt/hd/cityBottom" href="<?php echo $changeCityUrl;?>" class="react">
                <?php
                $cityList = Blyn::app()->getActiveApp()->getCityList();
                echo $cityList[$city];
                ?>
                <space></space>
                <i class="text-icon icon-downarrow"></i></a>
            </span>

<!--            <div >
                <?php // echo CHtml::beginForm() ?>
                <?php
                $screen = Blyn::app()->getUIManager()->getCurrentScreen();
                $cityList = Blyn::app()->getActiveApp()->getCityList();
                $model = new BDynamicFormModel();
                $model->addField('findText');
                $model->addField('userCity');
                //by default, set user city to ChongQing
                $model->userCity = 'china.chongqing';

                $savedUserCity = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId());
                if ($savedUserCity instanceof BAttribute) {
                    $model->userCity = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId())->getValue();
                }

                if ($_POST != NULL && isset($_POST['BDynamicFormModel'])) {
                    $model->attributes = $_POST['BDynamicFormModel'];
                }

                if (!$savedUserCity instanceof BAttribute || $savedUserCity->getValue() != $model->userCity) {
                    Blyn::app()->getCurrentUser()->addAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId(), $model->userCity);
                }
                ?>
                <div >
                    <div >
                        <?php // echo CHtml::activeDropDownList($model, 'userCity', $cityList, array('class' => 'span2')); ?>
                    </div>
                </div>
             
                <?php // echo CHtml::endForm() ?>
            </div>     -->
     </div>
        
        <!--刷新按钮-->
        <a class="refresh" href="<?php echo $bookingFindUrl;?>">
            <img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/refresh.png';?>" width="20" height="20"/>
        </a>
        <div id="ajax-loaderdiv" style="position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 35000;background-color: rgba(0, 0, 0, 0.3);display: none;">
            <img src="/billyn-dev/images/ajax-loader.gif" class="ajax-loader"style="position: absolute;left: 50%;top: 50%;margin-left: -16px;margin-top: -16px;display: block;"/>
        </div>
        <script>
        $(".refresh").click(function() {
            $("#ajax-loaderdiv").show();
        });
        </script>
        <?php
        $wxControl->renderView('booking_navigation',array());?>
    </header>
    <dl class="courtType_list">
        <dd class="courtType">
            <?php 
//            $eventList = $app->findEventList();
//            foreach ($eventList as $event) {
//                $vender = $event->getMessageManager()->getMessage('vender');
//                $bookableList = $vender->getMessageManager()->getMessageList();
                $courtTypeIcons = array(
                    'soccer_court_5' => Yii::app()->request->baseUrl . '/images/mobile/football.png',
                    'soccer_court_7' => Yii::app()->request->baseUrl . '/images/mobile/football.png',
                    'soccer_court_11' => Yii::app()->request->baseUrl . '/images/mobile/football.png',
                    'badminton_court' => Yii::app()->request->baseUrl . '/images/mobile/badminton2.png',
                );
                if(isset($findContext)) {
                    $findText = '';
                    $app->findByFindText($findText, $findContext);
                }
            ?>

            <ul style=" background-color: #60BF60; ">
                    <?php  foreach ($courtTypeList as $courtType => $courtName) {  
                        $findText = 'city=' . $city . ' courtType=' . $courtType;
                        $flagText = $courtName;
                        $count = $app->findCountByFindText($findText);
                        $courtUrl = $wxControl->getLinkUrl(array(
                            'name' => $courtName,
                            'id' => 'booking_list'), array(
                            'listPage' => 'booking_list_by_day',
                            'findText' => $findText,
                            'flagText' => $flagText,
                            'nowCity' => $city  //通常是省市拼音，如：china.chongqing
            //                            'findContext' => serialize(array('itemMatch' => '='))
                            ))
                        ?>
                        <li  style="width:50%; border: 1px; border-color:#2BB2A3;float:left; margin-top: 20px; margin-bottom: 20px;text-align: center;">
                            <a class="react" href="<?php echo $courtUrl;?>">
                            &nbsp;&nbsp;&nbsp;
                            <img src="<?php echo $courtTypeIcons[$courtType]; ?>" style="height:30px;width:30px;">
                            <em  style="font-size: 12px; margin-top: 10px;"class="num_court"><?php echo $count; ?></em>
                            <p style="font-size: small;text-align: inherit;line-height: 30px;"><?php echo $flagText;?></p>
                            </a>
                        </li>
                    <?php }?>
                </ul>
            <div style="width: 100%;height: 1px;clear: both;"></div>
            <?php // }?>
      </dd>
    </dl>
    
    
    <div class="more-weak" style="margin-bottom:.2rem;padding-left: 10px;"><!--class=more-->
        <strong style="font-size: 16px; float: bottom;  padding-top: 20px;;color: #F8B03A;margin-left: 9px;">热门推荐</strong>
    </div>
    
    <dl class="booking_list">
        <?php
        $itemData = array(
            'city' => $city,
            'bookingStatus' => 'available'
        );
        //设置黄金时间段
        $setStartOfTimeSlot = '>=170000';
        $setEndOfTimeSlot = '<=210000';
        $treeTable = new BTreeTable('BWInputValue');
        //valueList中有所有开始时间为键的预订项数量
        $valueList1 = $treeTable->getValueList('startTime', array(
            'itemData' => array_merge($itemData,array(
            'startOfTimeSlot' => $setStartOfTimeSlot,
            'endOfTimeSlot' => $setEndOfTimeSlot
                ))
            ));
        $valueList2 = $treeTable->getValueList('endTime', array(
            'itemData' => array_merge($itemData,array(
            'startOfTimeSlot' => $setStartOfTimeSlot,
            'endOfTimeSlot' => $setEndOfTimeSlot
                ))
            ));
        reset($valueList2);
        $kingCourtListArr = array();
        foreach ($valueList1 as $dateStamp => $count) {
            $week = date('l',$dateStamp);
            $startOfTimeSlot = date('H:i',$dateStamp);
            $endOfTimeSlot = date('H:i',key($valueList2));
           
            if(in_array($week,array('Monday', 'Tuesday' ,'Wednesday','Thursday','Friday'))) {
                $week = 'weekday';
                $isInValueArr = false;
                foreach ($kingCourtListArr as $key => $valueArr) {
                    if($valueArr['startOfTimeSlot']==$startOfTimeSlot && $valueArr['endOfTimeSlot']==$endOfTimeSlot && $valueArr['dayType']==$week) {
                        $kingCourtListArr[$key]['count'] = $valueArr['count'] + 1;
                        $isInValueArr = true;
                        break;
                    }
                }
                if(!$isInValueArr) {
                    $kingCourtListArr[] = array('startOfTimeSlot'=>$startOfTimeSlot,'endOfTimeSlot'=>$endOfTimeSlot,'dayType'=> 'weekday','count'=>$count);
                }
            }
            next($valueList2);
        }
        
        
        $valueList3 = $treeTable->getValueList('startTime', array('itemData' => $itemData));
        $valueList4 = $treeTable->getValueList('endTime', array('itemData' => $itemData));
        reset($valueList4);
        foreach ($valueList3 as $dateStamp => $count) {
            $week = date('l',$dateStamp);
            $startOfTimeSlot = date('H:i',$dateStamp);
            $endOfTimeSlot = date('H:i',key($valueList4));
           
            if(in_array($week,array('Saturday', 'Sunday'))) {
                $week = 'weekend';
                $isInValueArr = false;
                foreach ($kingCourtListArr as $key => $valueArr) {
                    if($valueArr['startOfTimeSlot']==$startOfTimeSlot && $valueArr['endOfTimeSlot']==$endOfTimeSlot && $valueArr['dayType']==$week) {
                        $kingCourtListArr[$key]['count'] = $valueArr['count'] + 1;
                        $isInValueArr = true;
                        break;
                    }
                }
                if(!$isInValueArr) {
                    $kingCourtListArr[] = array('startOfTimeSlot'=>$startOfTimeSlot,'endOfTimeSlot'=>$endOfTimeSlot,'dayType'=> 'weekend','count'=>$count);
                }
            }
            next($valueList4);
        }
        
        //循环遍历如下数组：array(1) ("2014-09-11"=> array("displayName"=> "今天" "count"=> 8 ) )
        foreach ($kingCourtListArr as $key => $valueArr) {
            //通过设置设置startTime和endTime把时间限定在某一天
            $startOfTimeSlot = $valueArr['startOfTimeSlot'];
            $endOfTimeSlot = $valueArr['endOfTimeSlot'];/*
            $itemData['startTime'] = '>='.$startTime;
            $itemData['endTime'] = '<='.$endTime;
            if($dateInfo['findType'] != 'allDay') {
                //把时间限定在某一时间区间
                $itemData['startOfTimeSlot'] = '>=180000';
                $itemData['endOfTimeSlot'] = '<=190000';
            }
                $searchValueList = $treeTable->getValueList('courtType', array('itemData' => $itemData));*/
                $findText = '';
                foreach ($itemData as $key => $value) {
                    $firstChar = substr($value, 0, 1);
                    if($firstChar == '=' || $firstChar == '>'|| $firstChar == '<') {
                        $findText = ($findText ? $findText." ".$key.$value : $key.$value);
                    }else {
                        $findText = ($findText ? $findText." ".$key."=".$value : $key."=".$value);
                    }
                }/*
                //去除字符串中第一个空格
                $findText = substr($findText,1,strlen($findText));*/
                $findText = $findText." startOfTimeSlot>=". str_replace(':00', '0000', $startOfTimeSlot)." endOfTimeSlot<=". str_replace(':00', '0000', $endOfTimeSlot);
                if($valueArr['dayType'] == 'weekday') {
                    $flagText = '工作日';
                    $findText = $findText.' startDay>=1'.' startDay<=5';
                } else {
                    $flagText = '周末';
                    $findText = $findText.' startDay>=6'.' startDay<=7';
                }
                
                $i = 0;
                $leftImageNO = 0;/*
                foreach ($searchValueList as $courtType => $countByType) {*/
                    $courtUrl = $wxControl->getLinkUrl(array(
                        'name' => $flagText,
                        'id' => 'booking_list'), array(
                        'listPage' => 'booking_list_1',
                        'findText' => $findText,
                        'flagText' => $flagText,
                        'nowCity' => $city  //通常是省市拼音，如：china.chongqing
                        ));
                    ?>

                    <dd style="border-bottom: .01rem solid #dedede;box-shadow: 0 .05rem .05rem #dedede;
                        margin-bottom:.15rem;
                        <?php /*if($i%2 != 0){echo 'background-color:#F3F3F3;';}else{*/echo 'background-color:white;';/*}*/?>">
                        <a href="<?php echo $courtUrl;?>">
                            <div class="item">
                                <table>
                                    <tr>
                                        <td style="width: 40px;height: 60px;">
                                            <img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/left_image'.$leftImageNO.'.png';?>" width="100%" height="100%;"/>
                                        </td>
                                        <td style="width: 1.5rem;height: 60px;vertical-align: middle;">
                                            <span style="color:2BB2A3;font-size:.4rem;font-weight:bold;"><?php echo $valueArr['dayType']=='weekday'?'工作日':'周末'; ?></span>
                                        </td>
                                        <td style="height: 60px;vertical-align: middle;">
                                            <img style="vertical-align:text-bottom;" src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/datetime.png';?>"/>
                                            <span style="color:#AFAFAF;font-size:16px;"><?php echo $startOfTimeSlot;?> - <?php echo $endOfTimeSlot;?></span>
                                            <br/>
                                            <?php // echo $courtTypeList[$courtType];?>
                                        </td>
                                        <td style="width:1.9rem;vertical-align:middle;">
                                             <span><a class="mbtn-trans" data-dealid="25150270">剩余场地：<font style=" font-family: fantasy;font-size: x-large; "><?php echo $valueArr['count'];?></font></a></span>
<!--                                            <div style="width:1.3rem;height: .36rem;padding-top:.04rem;background: url(<?php // echo Yii::app()->request->baseUrl.'/images/mobile/have.png';?>) no-repeat;background-size:100% 100%;">
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <span style="color:white;font-size: 16px;"></span>
                                            </div>

                                            <!--<img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/right24.png';?>"/>-->
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </a>
                    </dd>
                <?php 
                    if($leftImageNO < 2)
                        $leftImageNO++;
                    else 
                        $leftImageNO = 0;
                    $i++;
                }
                /*if(isset($itemData['startOfTimeSlot'])) {
                    unset($itemData['startOfTimeSlot']);
                }
                if(isset($itemData['endOfTimeSlot'])) {
                    unset($itemData['endOfTimeSlot']);
                }
        }*/
        ?>                
    </dl>        
    
    <footer>
        <div class="footer-copyright">
            <div class="hr"></div>
            <span class="footer-copyright-text">
                Copyright © 2014 billyn.net </span>
        </div>
    </footer>